/*
 *  Copyright (c) 2021@boostmerlin<https://github.com/boostmerlin/summer-boot>
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal
 *  in the Software without restriction, including without limitation the rights
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in all
 *  copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 *  SOFTWARE.
 */

package io.summer.gateway.handler;

import cn.hutool.http.HttpStatus;
import io.summer.basis.enums.IEnumCode;
import io.summer.basis.excepts.AppException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.boot.autoconfigure.web.WebProperties;
import org.springframework.boot.autoconfigure.web.reactive.error.DefaultErrorWebExceptionHandler;
import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.reactive.error.ErrorAttributes;
import org.springframework.context.ApplicationContext;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;

import java.util.Map;

/**
 * 网关自宝义异常处理
 * @author Moonlight
 */
@Slf4j
public class MyErrorWebExceptionHandler extends DefaultErrorWebExceptionHandler {
  public MyErrorWebExceptionHandler(ErrorAttributes errorAttributes, WebProperties webProperties,
                                    ErrorProperties errorProperties, ApplicationContext applicationContext) {
    super(errorAttributes, webProperties.getResources(), errorProperties, applicationContext);
  }

  @Override
  protected Map<String, Object> getErrorAttributes(ServerRequest request, ErrorAttributeOptions options) {
    Throwable error = getError(request);
    Map<String, Object> errorAttributes = super.getErrorAttributes(request, options);

    if (log.isErrorEnabled()) {
      log.error("MyErrorWebExceptionHandler: ", error);
    }
    // user aware app error return as OK.
    if (error instanceof AppException) {
      errorAttributes.put("code", ((AppException) error).getCode());
      errorAttributes.put("status", HttpStatus.HTTP_OK);
      errorAttributes.put("message", error.getMessage());
    } else {
      errorAttributes.put("code", IEnumCode.SERVER_ERROR);
    }
    errorAttributes.remove("trace");
    return errorAttributes;
  }

  @Override
  protected RouterFunction<ServerResponse> getRoutingFunction(ErrorAttributes errorAttributes) {
    return RouterFunctions.route(RequestPredicates.all(), this::renderErrorResponse);
  }
}
